package org.example.multiThreading.threadPool;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class TestScheduledThreadPool {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        /*
        ScheduledExecutorService
        定时线程池。指定核心线程数量，普通线程数量无限，线程执行完任务立即回收，
        任务队列为延时阻塞队列。这是一个比较特别的线程池，适用于执行定时或周期性的任务。
        */
        ScheduledExecutorService newed = Executors.newScheduledThreadPool(5);

        //延时任务
        TestCallable callable = new TestCallable();
        System.out.println(newed.schedule(callable, 1, TimeUnit.SECONDS).get());

        //定时任务
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("定时任务执行了");
            }
        };
        newed.scheduleAtFixedRate(runnable,2,2,TimeUnit.SECONDS);
    }
}
